package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.presenter.LoginPresenter;
import com.example.zbh.dsliao.ui.viewinterface.ViewRegisterInterface;
import com.example.zbh.dsliao.utils.PreferenceHelper;
import com.example.zbh.dsliao.view.CustomChangeDialog;
import com.example.zbh.dsliao.view.CustomDialog;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录页面
 */
public class LoginActivity extends MVPBaseActivity<ViewRegisterInterface, LoginPresenter> implements View.OnClickListener, ViewRegisterInterface {

    private Button loginBtn, registerBtn;
    private EditText et_login_mail, et_login_psw;
    private ImageView de_img_backgroud;
    private String userName = null;
    private String psw = null;
    private CustomDialog customDialog;
    private CustomChangeDialog dialog;



    @Override
    protected int getLayOut() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        registerBtn = findeView(R.id.registerBtn);//注册按钮

        loginBtn = findeView(R.id.loginBtn);//登录按钮
        loginBtn.getBackground().setAlpha(100);//设置透明度
        registerBtn.getBackground().setAlpha(100);
        et_login_psw = findeView(R.id.et_login_psw);//输入密码
        et_login_mail = findeView(R.id.et_login_mail);//输入邮箱
        de_img_backgroud = findeView(R.id.de_img_backgroud);//背景
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.background_anim);
        de_img_backgroud.startAnimation(animation);//背景动画
    }

    @Override
    protected void initData() {
        registerBtn.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        //初始化dialog
        customDialog = new CustomDialog(this, "玩命加载中...");
        dialog = new CustomChangeDialog(this, "是否保存用户名及密码",View.GONE,"","确定","否");
        userName = PreferenceHelper.getInstance().getCurrentUserName();
        psw = PreferenceHelper.getInstance().getCurrentUserPsw();

        if (userName != null && psw != null){
            et_login_mail.setText(userName);
            et_login_psw.setText(psw);
            Map<String, String> map = new HashMap<>();
            map.put(Constant.USER_NAME, userName);
            map.put(Constant.USER_PSW, psw);
            createPresenter().startLogin(map);
            customDialog.show();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.registerBtn://注册按钮
                Intent inten = new Intent(this, RegisterActivity.class);
                startActivity(inten);
                finish();
                break;
            case R.id.loginBtn:
                showLoginDialog();
                break;
        }
    }

    private void showLoginDialog() {
        dialog.show();
        dialog.setCustomChangeDialogListener(new CustomChangeDialog.CustomChangeDialogListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.changeTwoBtn:
                        getUserinfo();
                        dialog.dismiss();
                        break;
                    case R.id.cancleBtn:
                        getUserinfo();
                        dialog.dismiss();
                        PreferenceHelper.getInstance().removeCurrentUserPsw();
                        PreferenceHelper.getInstance().removeCurrentUserName();
                        break;
                }
            }
        });




    }

    private void getUserinfo() {
        customDialog.show();//展示dialog
        userName = et_login_mail.getText().toString().trim();
        psw = et_login_psw.getText().toString().trim();
        PreferenceHelper.getInstance().setCurrentUserName(userName);
        PreferenceHelper.getInstance().setCurrentUserPsw(psw);
        Map<String, String> map = new HashMap<>();
        map.put(Constant.USER_NAME, userName);
        map.put(Constant.USER_PSW, psw);
        createPresenter().startLogin(map);

    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }


    //登录成功
    @Override
    public void showResult(String str) {
        customDialog.dismiss();
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void ShowSucResult() {



    }
    //登录失败
    @Override
    public void showFailResult(String str) {
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
        PreferenceHelper.getInstance().removeCurrentUserName();
        PreferenceHelper.getInstance().removeCurrentUserPsw();
        customDialog.dismiss();
    }
}
